package inet.app.vod.activity.common;

import inet.app.vod.activity.R;
import inet.app.vod.config.AppConstants;

import java.util.ArrayList;

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;

import com.inet.app.framework.util.Log;

public abstract class GestureActivity extends BaseActivity implements OnGesturePerformedListener {
	
	protected GestureLibrary mGestureLibrary;
	protected GestureOverlayView mGestureView;
	
	protected abstract void play();
	
	protected abstract void pause();
	
	protected abstract void plusVolume();
	
	protected abstract void minusVolume();
	
	protected abstract void capture();
	
	protected abstract void mute();
	
	protected abstract void scale();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mGestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if(mGestureLibrary != null) {
        	if(!mGestureLibrary.load()) {
        	     Log.e("Gesture library was not loaded…");
        	     finish();
        	}
        }
	}
	
	@Override
	public void onGesturePerformed(GestureOverlayView view, Gesture gesture) {
		ArrayList<Prediction> predictions = mGestureLibrary.recognize(gesture);
		 
		// one prediction needed
		if (predictions.size() > 0) {
			Prediction prediction = predictions.get(0);
			if (prediction.score > 1.0) {
				String pName = prediction.name;
				
				if(AppConstants.GESTURE_PLAY.equals(pName)) {
					Log.d("gesture play");
					play();
				}
				else if(AppConstants.GESTURE_PAUSE.equals(pName)) {
					Log.d("gesture pause");
					pause();
				}
				else if(AppConstants.GESTURE_PLUS_VOLUME.equals(pName)) {
					Log.d("gesture plus volume");
					plusVolume();
				}
				else if(AppConstants.GESTURE_MINUS_VOLUME.equals(pName)) {
					Log.d("gesture minus volume");
					minusVolume();
				}
				else if(AppConstants.GESTURE_MUTE.equals(pName)) {
					Log.d("gesture mute");
					mute();
				}
				else if(AppConstants.GESTURE_SCALE.equals(pName)) {
					Log.d("gesture scale");
					scale();
				}
				else if(AppConstants.GESTURE_CAPTURE.equals(pName)) {
					Log.d("gesture capture");
					capture();
				}
 			}
		}	
		
	}
}
